package com.jdd.fintech.morse.plugin.util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;

/**
 * <b>Description</b>:被动核算——金条加密工具类<br>
 *
 * <b>Author</b>:fengcheng<br>
 * <b>Date</b>:2021/02/22 <br>
 */
public class AesUtils {
    private static final String[] HEX_DIGITS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

    /**
     * 加密
     * @param encryptKey
     * @param plainTextData
     * @return
     * @throws Exception
     */
    public static String encryptAES(String encryptKey, String plainTextData) throws Exception {
        Cipher cipher = Cipher.getInstance( "AES/CBC/PKCS5Padding" );
        byte[] raw = encryptKey.getBytes("utf-8");
        IvParameterSpec iv = new IvParameterSpec(raw);
        cipher.init( Cipher.ENCRYPT_MODE, new SecretKeySpec( raw, "AES" ), iv );
        byte[] encryptedData = cipher.doFinal( plainTextData.getBytes( "UTF-8" ) );
        return Base64.encodeBase64String( encryptedData );
    }

    /**
     * 解密
     * @param encryptKey
     * @param encryptedData
     * @return
     * @throws Exception
     */
    public static String decryptAES(String encryptKey, String encryptedData) throws Exception {
        Cipher cipher = Cipher.getInstance( "AES/CBC/PKCS5Padding" );
        byte[] raw = encryptKey.getBytes("utf-8");
        IvParameterSpec iv = new IvParameterSpec(raw);
        cipher.init( Cipher.DECRYPT_MODE, new SecretKeySpec( raw, "AES" ), iv );
        byte[] decryptedData = cipher.doFinal( Base64.decodeBase64( encryptedData ) );
        return new String( decryptedData, "UTF-8" );
    }

    /**
     * 处理文件下载时候的编码问题
     * @param encryptKey
     * @param encryptedData
     * @return
     * @throws Exception
     */
    public static String decryptAESForFile(String encryptKey, String encryptedData) throws Exception {
        Cipher cipher = Cipher.getInstance( "AES/CBC/PKCS5Padding" );
        byte[] raw = encryptKey.getBytes("utf-8");
        IvParameterSpec iv = new IvParameterSpec(raw);
        cipher.init( Cipher.DECRYPT_MODE, new SecretKeySpec( raw, "AES" ), iv );
        byte[] decryptedData = cipher.doFinal( Base64.decodeBase64( encryptedData ) );
        return Base64.encodeBase64String(decryptedData);
    }

    /**
     * 签名
     * @param data
     * @param salt
     * @return
     * @throws Exception
     */
    public static String sign(String data, String salt) throws Exception {
        data = data + "|" + salt;
        byte[] bytes = MessageDigest.getInstance( "MD5" ).digest( data.getBytes( "UTF-8" ) );
        return byteToString( bytes );
    }

    private static String byteToString(byte[] bByte) {
        StringBuilder sb = new StringBuilder();
        for (byte aBByte : bByte) {
            sb.append(byteToArrayString(aBByte));
        }
        return sb.toString();
    }

    private static String byteToArrayString(byte bByte) {
        int iRet = bByte;
        if (iRet < 0) {
            iRet += 256;
        }
        int iD1 = iRet / 16;
        int iD2 = iRet % 16;
        return HEX_DIGITS[iD1] + HEX_DIGITS[iD2];
    }

    public static void main(String[] args) throws Exception {
        System.out.println(222);

        String salt="123321123";
        String requestDate="FAhtOHhphIGfIqyDVdEXjmF6scKR68rcvhqEUkksSTY=";
        String sign = sign(requestDate,salt);
        System.out.println(sign);
    }
}
